import axios from '@/libs/request/axios'
import QS from 'qs';
import {setStore,getStore} from "@/libs/utils/store";
import Constants from "@/const/common/Constants"
// 获取全部字典
export const getAllDictList = (params) => {
    return axios.request({
        url: '/admin/dict/getAll',
        params: params,
        method: 'get'
    })
}

// 编辑字典
export const editDict = (params) => {
    return axios.request({
        url: '/admin/dict/edit',
        data: params,
        method: 'post'
    })
}
// 编辑字典
export const addDict = (params) => {
    return axios.request({
        url: '/admin/dict/add',
        data: params,
        method: 'post'
    })
}
// 删除字典
export const deleteDict = (ids, params) => {
    return axios.request({
        url: `/admin/dict/delByIds/${ids}`,
        params: params,
        method: 'delete'
    })
}
// 搜索字典
export const searchDict = (params) => {
    return axios.request({
        url: '/admin/dict/search',
        params: params,
        method: 'get'
    })
}
// 获取全部字典数据
export const getAllDictDataList = (params) => {
    return axios.request({
        url: '/admin/dictdata/getByCondition',
        params: params,
        method: 'get'
    })
}
// 添加字典数据
export const addDictData = (params) => {
    return axios.request({
        url: '/admin/dictdata/add',
        data: params,
        method: 'post'
    })
}
// 编辑字典数据
export const editDictData = (params) => {
    return axios.request({
        url: '/admin/dictdata/edit',
        data: params,
        method: 'post'
    })
}

// 删除字典数据
export const deleteData = (ids, params) => {
    return axios.request({
        url: `/admin/dictdata/delByIds/${ids}`,
        params: params,
        method: 'delete'
    })
}

// 获取缓存数据字典
export const listAllCacheDicData = () => {
    axios.request({
        url: '/admin/dictdata/listAll',
        method: 'post'
    }).then(res=>{
        if(res.code==='0000'){
            setStore({name:Constants.dic_cache,type:Constants.local_store_type,content:res.data})
        }
    })
}
// 通过类型获取字典数据
export const getDictDataByType = (type, params) => {
    let cacheValue = getStore({name:Constants.dic_cache})
    if(cacheValue&&cacheValue[type]){
        return new Promise(reslove=>{
            reslove({
                code:'0000',
                data:cacheValue[type]
            })
        })
    }
    return axios.request({
        url:`/admin/dictdata/getByType/${type}`,
        params: params,
        method: 'get'
    })
}

// 获取多个类型数据字典值
export const getByTypes = (typeArray) => {
    let cacheValue = getStore({name:Constants.dic_cache});
    let hitCache = true;
    let hitCacheValue={};
    for(let index=0;index<typeArray.length;index++){
        if(!cacheValue||!cacheValue[typeArray[index]]){
            hitCache = false;
            break;
        }else {
            hitCacheValue[typeArray[index]] = cacheValue[typeArray[index]];
        }
    }
    if(hitCache){
        return new Promise(resolve=>{
            resolve({
                code:'0000',
                data:hitCacheValue
            })
        })
    }
    return axios.request({
        url: '/admin/dictdata/getByTypes',
        data:QS.stringify({types:typeArray},{arrayFormat: 'repeat'}),
        headers: { 'content-type': 'application/x-www-form-urlencoded' },
        method: 'post'
    })
}
